package com.htt.app.amnhac12h.view;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.htt.app.amnhac12h.AmNhac12h;
import com.htt.app.amnhac12h.R;
import com.htt.app.amnhac12h.adapter.ListFavoriteAdapter;
import com.htt.app.amnhac12h.object.Show;
import com.htt.app.amnhac12h.sqlite.DAOShow;

public class FavoriteView implements OnClickListener{
	public boolean isCreate = false;
	
	private ListView list;
	private Activity activity;

	private ImageView btnHome;
	private TextView tvTitle;
	private ImageView btnBack;
	
	private View view;
	
	public View createView(Activity context, List<Show> data) {
		// TODO Auto-generated constructor stub
		activity = context;
		view = LayoutInflater.from(context).inflate(R.layout.list, null);
		
		list = (ListView) view.findViewById(R.id.list);
		btnHome = (ImageView) view.findViewById(R.id.list_header_refresh);
		tvTitle = (TextView) view.findViewById(R.id.list_header_title);
		
		/*
		if(CategoriesView.statusView == 1 && Radio.viewDisplay == Radio.VIEWCATEGORIES) {
			btnBack = (ImageView) view.findViewById(R.id.list_header_back);
			btnBack.setVisibility(View.VISIBLE);
			btnBack.setOnClickListener(this);
		}*/
		
		btnHome.setOnClickListener(this);
		
		setData(data);
		isCreate = true;
		
		return view;
	}
	
	/*
	 *  set data for display
	 */
	
	public void setData(List<Show> data) {
		if(data != null && data.size() > 0) {
			tvTitle.setText(R.string.title_channel);
			
			ListFavoriteAdapter adapter = new ListFavoriteAdapter(activity, R.layout.list_show_item, data);
			
			list.setAdapter(adapter);
		}
		
	}
	
	public void setTitle(String title) {
		tvTitle.setText(title);
	}
	
	public View getView() {
		// refresh  list view
		DAOShow db = new DAOShow(activity);
		db.open();
		List<Show> data = db.getAllShow();
		db.close();
		setData(data);
		
		return view;
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.list_header_refresh:
			home();
			break;
			
		default:
			break;
		}
	}

	private void home() {
		AmNhac12h.home_view.removeAllViews();
		if(AmNhac12h.playerView.isCreate)
			AmNhac12h.home_view.addView(AmNhac12h.playerView.getView());
		else
			AmNhac12h.home_view.addView(AmNhac12h.playerView.createView(activity,null));
		
		AmNhac12h.changeStatusViewDisplay(AmNhac12h.VIEWPLAYER);
	}
}
